import os

outerdir = ['addons', 'js-native-api', 'node-api']
casedir = []

for i in outerdir:
    os.chdir(i)
    lines = []
    failed = []
    innerdir = [x for x in os.listdir() if os.path.isdir(x)]
    innerdir.remove('__pycache__')

    lines.append(f'# {i}\n\n')
    lines.append(f'## Test cases\n\n')

    for dir in innerdir:
        os.chdir(dir)
        cases = [x for x in os.listdir() if x.endswith('.js')]
        for case in cases:
            with open(f'{case}.exitcode', 'r') as f:
                code = f.readline()
                if code != '0':
                    failed.append(f'{dir}/{case}')

            lines.append(f'{dir}/{case}\n\n')

        os.chdir('..')

    lines.append(f'\n## Not pass cases\n\n')
    for case in failed:
        lines.append(f'{case}\n\n')

    lines.append(f'\n## Details\n\n')

    for dir in innerdir:
        os.chdir(dir)
        cases = [x for x in os.listdir() if x.endswith('.js')]
        for case in cases:
            try:
                lines.append(f'### {dir}/{case}\n\n')

                with open(f'{case}.exitcode', 'r') as f:
                    code = f.readline()
                    lines.append(f'Exit code: {code}\n\n')

                with open(f'{case}.out', 'r', encoding='utf-8') as f:
                    if f.read() == '':
                        pass
                    else:
                        lines.append('stdout:\n\n')
                        lines.append(f'```text\n')
                        f.seek(0)
                        lines.extend(f.readlines())
                        lines.append(f'\n```\n\n')

                with open(f'{case}.err', 'r', encoding='utf-8') as f:
                    if f.read() == '':
                        pass
                    else:
                        lines.append('stderr:\n\n')
                        lines.append(f'```text\n')
                        f.seek(0)
                        lines.extend(f.readlines())
                        lines.append(f'\n```\n\n')

            except FileNotFoundError:
                pass

        os.chdir('..')
    os.chdir('..')
    with open(f'{i}.md', 'w', encoding='utf-8') as f:
        f.writelines(lines)
